﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace test
{
    public struct Point2D
    {
        public double X, Y;
        public Point2D(double x, double y)
        {
            this.X = x;
            this.Y = y;
        }
        public static Point2D operator +(Point2D p1, Point2D p2)
        {
            return new Point2D(p1.X + p2.X, p1.Y + p2.Y);
        }
        public static Point2D operator -(Point2D p1, Point2D p2)
        {
            return new Point2D(p1.X - p2.X, p1.Y - p2.Y);
        }
        public static Point2D operator /(Point2D p1, int n)
        {
            return new Point2D(p1.X / n, p1.Y / n);
        }
    }

    public class rotate
    {
        public double cosa = 0.0;
        public double sina = 0.0;
        public double offsetX = 0.0;
        public double offsetY = 0.0;
        public rotate(Point2D oldp1, Point2D oldp2, Point2D newp1, Point2D newp2)
        {
            double Q = oldp1.X - oldp2.X;
            double W = oldp1.Y - oldp2.Y;
            double M = newp1.X - newp2.X;
            double N = newp1.Y - newp2.Y;
            
            sina = (W * M - N * Q) / (M * M + N * N);
            cosa = ((N * sina) + Q) / M;
            offsetX = oldp1.X - newp1.X * cosa + newp1.Y * sina;
            offsetY = oldp1.Y - newp1.X * sina - newp1.Y * cosa;
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            test();
        }

        public void test()
        {
            //301411 301412大地&实测
            //Point2D baseCp1 = new Point2D(506797.3006, 4386837.7752);
            //Point2D baseCp2 = new Point2D(506788.3229, 4386833.5391);
            //Point2D surveyCp1 = new Point2D(10.0955, 0.65903);
            //Point2D surveyCp2 = new Point2D(0.31415, 1.36752);
            //Point2D surveyGp1 = new Point2D(4.63196, 4.58437);

            Point2D baseCp1 = new Point2D(506825.4836, 4386778.8073);
            Point2D baseCp2 = new Point2D(506816.5453, 4386774.5198);
            Point2D surveyCp1 = new Point2D(20.3236, 93.7425);
            Point2D surveyCp2 = new Point2D(10.5913, 95.6085);
            Point2D surveyGp1 = new Point2D(15.8725, 83.1016);

            rotate ro = new rotate(baseCp1, baseCp2, surveyCp1, surveyCp2);
            Point2D pt = PNTrotate(surveyGp1, ro);
            MessageBox.Show(pt.X + "," + pt.Y);
        }

        public Point2D PNTrotate(Point2D pt, rotate r)
        {
            double x = pt.X * r.cosa - pt.Y * r.sina + r.offsetX;
            double y = pt.X * r.sina + pt.Y * r.cosa + r.offsetY;
            return new Point2D(x, y);
        }


    }
}